/**
 * http://mysite.du.edu/~jcalvert/math/ellipse.htm
 **/


package {
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	/**
	 * Found planetary facts at http://solarsystem.jpl.nasa.gov/planets/index.cfm
	 * 
	 **/
	public class SolarSytem extends Sprite
	{
		public var Sol:Object = {
			name:"Sol",
			diameter:109,	//X earth
			mass:332900,	//X earth
			day:25.38,		//in earth days
			satellites:[
				{
					name:"Mercury",
					periapsis:0.3075,	//A.U.
					apoapsis:0.4667,	//A.U.
					diameter:0.3825,	//X Earth diam
					mass:0.055,			//X Earth mass
					day:58.64,			//X Earth day
					year:0.241,			//X Earth year
					incline:7,			//Degrees
					tilt:0,				//Degrees
					satellites:[]
				},
				{
					name:"Venus",
					periapsis:0.718,
					apoapsis:0.728,
					diameter:0.9488,
					mass:0.815,
					day:-243,
					year:0.615,
					incline:3.39,
					tilt:177.3,
					satellites:[]
				},
				{
					name:"Earth",
					periapsis:0.983,
					apoapsis:1.017,
					diameter:1,
					mass:1,
					day:1,
					year:1,
					incline:0.00005,
					tilt:23.45,
					satellites:[
						{
							name:"The Moon",
							periapsis:0.00243,
							apoapsis:0.00271,
							diameter:0.2724,
							mass:0.0123,
							day:27.321661,
							year:0.075,
							incline:5.145,
							tilt:6.68,
							satellites:[]
						}
					]
				},
				{
					name:"Mars",
					periapsis:1.381,
					apoapsis:1.666,
					diameter:0.5326,
					mass:0.10744,
					day:1.026,
					year:1.8807,
					incline:1.8,
					tilt:25.19,
					satellites:[] //There are 2, Phobos & Deimos...  want to get me data?
				},
				{
					name:"Jupiter",
					periapsis:4.952,
					apoapsis:5.455,
					diameter:11.209,
					mass:317.82,
					day:0.41354,
					year:11.8565,
					incline:1.305,
					tilt:3.12,
					satellites:[] //There are 63!  I need to get more data!
				},
				{
					name:"Saturn",
					periapsis:9.021,
					apoapsis:10.054,
					diameter:9.449,
					mass:95.16,
					day:0.44401,
					year:29.4,
					incline:2.484,
					tilt:26.73,
					satellites:[] //There are 62!  Am I REALLY going to enter all that in here?
				},
				{
					name:"Uranus", //Changed to Urectum in the year 2620
					periapsis:18.286,
					apoapsis:20.096,
					diameter:4.007,
					mass:14.371,
					day:-0.7196,
					year:84.02,
					incline:0.770,
					tilt:97.86,
					satellites:[] //There are 27...
				},
				{
					name:"Neptune",
					periapsis:29.811,
					apoapsis:30.327,
					diameter:3.883,
					mass:17.147,
					day:0.67125,
					year:164.79,
					incline:1.769,
					tilt:29.58,
					satellites:[] //There are 13...
				},
				{
					name:"Pluto",
					periapsis:29.658,
					apoapsis:49.305,
					diameter:0.180,
					mass:0.0022,
					day:-6.387,
					year:247.92,
					incline:17.14,
					tilt:119.61,
					satellites:[] //There are 3.  Hydra and Nix, as well as its companion moon, Charon
				},
			]
		};
		public var scale:Number = 50;
		public var p:Point = new Point(200,200);
		
		public function SolarSytem()
		{
			var g:Graphics = this.graphics;
			g.lineStyle(2,0xFF0000,1);
			g.moveTo(p.x,p.y);
			g.lineTo(p.x+1,p.y+1);
			g.endFill();
			
			var planet:Object;
			for(var i:int=0; i<Sol.satellites.length; i++)
			{
				planet = Sol.satellites[i];
				drawOrbit(planet.apoapsis, planet.periapsis);
			}
		}
			
		public function drawOrbit(apo:Number, peri:Number):void
		{
			var c:Number = (apo - peri)*scale;
			var a:Number = c*.5 + peri*scale;
			var b:Number = Math.sqrt(a*a - c*c);
			
			var h:Number = 2*b;
			var w:Number = 2*a;
			var px:Number = p.x - peri*scale;
			var py:Number = p.y - b;
			
			var g:Graphics = this.graphics;
			g.lineStyle(1,0,1);
			g.drawEllipse(px, py, w, h);
			g.endFill();
		}
	}
}
